package com.yatang.yiliao.app.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.yatang.yiliao.app.Constants;
import com.yatang.yiliao.app.R;
import com.yatang.yiliao.app.utils.MyCountTimer;



public class PhoneRegisterActivity extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private EditText txtPhone;
    private EditText txtPassword;
    private Button authCode;
    private Button register;
    private CheckBox acceptProtocol;
    private String phoneNum;
    private String password;
    private boolean isAcceptProtocol;
    public static final int TIME_COUNT = 61000;//时间防止从59s开始显示
    private MyCountTimer time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_register);

        TextView txtProtocol = (TextView)findViewById(R.id.txt_protocol);
        txtProtocol.setOnClickListener(this);

        txtPhone = (EditText)findViewById(R.id.txt_phone_register);
        txtPassword = (EditText)findViewById(R.id.txt_password_register);

        authCode = (Button)findViewById(R.id.btn_auth_code);
        authCode.setOnClickListener(this);
        time = new MyCountTimer(TIME_COUNT,1000);
        time.setBtn(authCode);

        register = (Button)findViewById(R.id.btn_register);
        register.setOnClickListener(this);

        acceptProtocol = (CheckBox)findViewById(R.id.cb_accept_protocol);
        SharedPreferences sp=getSharedPreferences(Constants.APP_DATA,MODE_PRIVATE);
        boolean aBoolean = sp.getBoolean(Constants.APP_IS_ACCEPT_PROTOCOL, true);
        isAcceptProtocol = aBoolean;
        acceptProtocol.setChecked(aBoolean);
        acceptProtocol.setOnCheckedChangeListener(this);

    }


    @Override
    public void onClick(View v) {
        phoneNum = txtPhone.getText().toString();
        password = txtPassword.getText().toString();
        int id = v.getId();
        switch (id){
            case R.id.txt_protocol:
                Intent intent = new Intent(this,ProtocolActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_auth_code:

                if (phoneNum.length() == 11) {
                    AlertDialog.Builder builder
                            = new AlertDialog.Builder(this);
                    builder.setTitle("提示")
                            .setMessage("我们将发送验证码到手机号：+86"+phoneNum)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    time.start();
                                    // 对话框消失
                                    dialog.dismiss();
                                }
                            })  // 设置确定按钮，第二个参数就是确定按钮点击的对话框点击事件处理
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // 对话框消失
                                    dialog.dismiss();
                                }
                            }) // 设置否定按钮（取消按钮）
                                    // 设置对话框的图标
                            .setIcon(R.drawable.ic_launcher)
                    ;
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }else {
                    Toast.makeText(this,"请输入正确的手机号码！",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_register:
                if (isAcceptProtocol){
                    if (phoneNum.length()==11){
                        if (password.length()>=6){
                            //TODO 处理验证码和发送注册信息到服务器
                            Toast.makeText(this,"注册成功！",Toast.LENGTH_SHORT).show();
                            Intent intent1 = new Intent(this,MainActivity.class);
                            startActivity(intent1);
                            finish();
                        }else {
                            Toast.makeText(this,"密码长度不足6位！",Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(this,"请输入正确的手机号码！",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this,"请阅读并接受《用户许可协议》后注册！",Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        isAcceptProtocol = isChecked;
        SharedPreferences sp=getSharedPreferences(Constants.APP_DATA,MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(Constants.APP_IS_ACCEPT_PROTOCOL,isChecked);
        edit.commit();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SharedPreferences sp=getSharedPreferences(Constants.APP_DATA,MODE_PRIVATE);
        boolean aBoolean = sp.getBoolean(Constants.APP_IS_ACCEPT_PROTOCOL, true);
        isAcceptProtocol = aBoolean;
        acceptProtocol.setChecked(aBoolean);
    }


}
